/**
 * Desenvolvido pela Fábrica de Software do CESUPA.
 * Todos os direitos reservados.
 */
package br.cesupa.fabsoft.nomedoprojeto.modules.helpmodule;

import br.cesupa.fabsoft.nomedoprojeto.modules.helpmodule.entities.HelpTopic;
import br.cesupa.fabsoft.nomedoprojeto.modules.helpmodule.services.HelpService;
import br.cesupa.fabsoft.nomedoprojeto.utils.FileUtils;
import java.io.File;
import java.io.IOException;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;

/**
 *
 * @author Breno Leite
 */
public class HelpIndexer {

    private IndexWriter writer;
    private Directory directory;
    private Analyzer analyzer = new StandardAnalyzer();

    public HelpIndexer() {
    }

    public void indexDefaltDirectory() {
        try {
            directory = FSDirectory.getDirectory(HelpService.getHelpIndexDirectory(), true);
            writer = new IndexWriter(directory, analyzer, true);
            indexDirectory(HelpService.getHelpContentDirectory());
            writer.close();
        } catch (IOException exception) {
            exception.printStackTrace();
        }
    }

    public void indexDirectory(File targetFile) {
        for (int i = 0; i < targetFile.listFiles().length; i++) {
            File tempFile = targetFile.listFiles()[i];
            if (tempFile.isDirectory()) {
                indexDirectory(tempFile);
            } else {
                try {
                    Document document = new Document();

                    if (HelpService.getAcceptedHelpExtensions().contains(
                            FileUtils.extractExtensionFromFileName(tempFile.getName()))) {

                        HelpTopic helpEntry = HelpService.retriveFileInfo(tempFile);

                        document.add(new Field("path", helpEntry.getPath(),
                                Field.Store.YES, Field.Index.NO));

                        if (helpEntry.getTitle() != null) {
                            document.add(new Field(
                                    "title", helpEntry.getTitle(),
                                    Field.Store.YES,
                                    Field.Index.TOKENIZED));
                        }

                        if (helpEntry.getDescription() != null) {
                            document.add(new Field(
                                    "description", helpEntry.getDescription(),
                                    Field.Store.YES,
                                    Field.Index.NO));
                        }

                        if (helpEntry.getKeywords() != null) {
                            document.add(new Field(
                                    "keywords", helpEntry.getKeywords(),
                                    Field.Store.YES,
                                    Field.Index.TOKENIZED));
                        }

                        document.add(new Field("content",
                                helpEntry.getContent(),
                                Field.Store.YES, Field.Index.TOKENIZED));
                    }

                    writer.addDocument(document);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}